﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using DAL = AccessDAL;
using CommonFunc;

public partial class Manageweb_ManageINews_ManageINewsClass : System.Web.UI.Page
{
    DAL.INewsClassTableAdapters.INewsClassTableAdapter DINC =
        new DAL.INewsClassTableAdapters.INewsClassTableAdapter();

    MODEL.INewsClass MINC = new MODEL.INewsClass();
    MODEL.INewsClass _MINC = new MODEL.INewsClass();

    BLL.INewsClass BINC = new BLL.INewsClass();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.Cookies["MasterID"] == null)
        {
            Response.Redirect("../Default.aspx");
        }

        if (!IsPostBack)
        {
            DDL_ClassDataBind();
            PageDataInit();
        }

        ClickInit();
    }


    #region ClickInit [初始化]
    protected void ClickInit()
    {
        LB_ManageINewsClass.Text = "";
        TABLE_Add_Mod.Visible = false;

        try
        {
            if (LB_ID.Text == "")
            {
                Btn_Submit.Click += new EventHandler(Btn_Add_Click);
                LB_TableTitle.Text = "添加类别";
            }
            else
            {
                Btn_Submit.Click += new EventHandler(Btn_Mod_Click);
                LB_TableTitle.Text = "修改类别";
            }
        }
        catch (Exception ex)
        {
            LB_ManageINewsClass.Text = ex.Message;
        }
    }
    #endregion


    #region 页面数据初始化
    public void PageDataInit()
    {
        try
        {
            if (LB_ID.Text == "")
            {
                TB_Title.Text = "";
                DDL_Class.Enabled = true;
                DDL_Class.SelectedValue = "";
            }
            else
            {
                MINC.INC_ID = Convert.ToInt32(LB_ID.Text);
                _MINC = BINC.GetDataByID(MINC);

                TB_Title.Text = _MINC.INC_Title;
                DDL_Class.SelectedValue = _MINC.INC_ParentID.ToString();
                DDL_Class.Enabled = false;
            }
        }
        catch (Exception ex)
        {
            LB_ManageINewsClass.Text = ex.Message;
        }
    }
    #endregion


    #region DDL_Class数据绑定 [初始化]
    protected void DDL_ClassDataBind()
    {
        try
        {
            DDL_Class.Items.Clear();

            ListItem LI0 = new ListItem("", "");
            DDL_Class.Items.Add(LI0);
            ListItem LI1 = new ListItem("根目录", "0");
            DDL_Class.Items.Add(LI1);

            DataTable DT = DINC.GetDataOrderByClassCode(false);

            foreach (DataRow dr in DT.Rows)
            {
                ListItem li = new ListItem(
                    ClassCode.CreateClassListItemText(dr["INC_Title"].ToString(), dr["INC_Level"].ToString()),
                    dr["INC_ID"].ToString());

                DDL_Class.Items.Add(li);
            }
        }
        catch (Exception ex)
        {
            LB_ManageINewsClass.Text = ex.Message;
        }
    }
    #endregion


    #region 类别标题 [格式化]
    protected string FormatClassTitle(object _Title, object _Level)
    {
        return ClassCode.CreateClassListItemText(_Title.ToString(), _Level.ToString());
    }
    #endregion


    #region 添加类别 [GridView] [Command]
    protected void LBtn_Add_Command(object sender, CommandEventArgs e)
    {
        try
        {
            TABLE_Add_Mod.Visible = true;

            LB_ID.Text = "";

            PageDataInit();
        }
        catch (Exception ex)
        {
            LB_ManageINewsClass.Text = ex.Message;
        }
    }
    #endregion
    #region 修改类别 [GridView] [Command]
    protected void LBtn_Mod_Command(object sender, CommandEventArgs e)
    {
        try
        {
            TABLE_Add_Mod.Visible = true;

            LB_ID.Text = e.CommandArgument.ToString();

            PageDataInit();
        }
        catch (Exception ex)
        {
            LB_ManageINewsClass.Text = ex.Message;
        }
    }
    #endregion
    #region 删除类别 [GridView] [Command]
    protected void LBtn_Del_Command(object sender, CommandEventArgs e)
    {
        try
        {
            MINC.INC_ID = Convert.ToInt32(e.CommandArgument);

            BINC.Del(MINC);

            LB_ManageINewsClass.Text = "删除成功";

            GV_INewsClass.DataBind();

            DDL_ClassDataBind();
        }
        catch (Exception ex)
        {
            LB_ManageINewsClass.Text = ex.Message;
        }
    }
    #endregion


    #region 添加类别 [Click]
    protected void Btn_Add_Click(object sender, EventArgs e)
    {
        try
        {
            MINC.INC_Title = TB_Title.Text;
            MINC.INC_ID = Convert.ToInt32(DDL_Class.SelectedValue);
            MINC.INC_Thumbnail = "";
            MINC.INC_IsLock = false;

            BINC.Add(MINC);

            LB_ManageINewsClass.Text = "添加成功";

            GV_INewsClass.DataBind();

            DDL_ClassDataBind();
        }
        catch (Exception ex)
        {
            LB_ManageINewsClass.Text = ex.Message;
        }
    }
    #endregion


    #region 修改类别 [Click]
    protected void Btn_Mod_Click(object sender, EventArgs e)
    {
        try
        {
            MINC.INC_ID = Convert.ToInt32(LB_ID.Text);
            MINC.INC_Title = TB_Title.Text;

            BINC.Mod(MINC);

            GV_INewsClass.DataBind();

            DDL_ClassDataBind();

            LB_ManageINewsClass.Text = "修改成功";
        }
        catch (Exception ex)
        {
            LB_ManageINewsClass.Text = ex.Message;
        }
    }
    #endregion
}
